﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.Serialization;

namespace Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_Chose_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "JPG |*.jpg| ALl File|*.*";
            openFileDialog1.InitialDirectory = "C:\\";
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    comboBox1.Text = openFileDialog1.FileName;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }

        }

        private byte[] ImageToByteArray(System.Drawing.Image imageIn)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                imageIn.Save(ms, ImageFormat.Jpeg);
                return ms.ToArray();
            }
        }

        public Image ByteArrayToImage(byte[] byteArrayIn)
        {
            using (MemoryStream ms = new MemoryStream(byteArrayIn))
            {
                Image returnImage = Image.FromStream(ms);
                return returnImage;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\DB1.accdb");
            con.Open();
            string sql = "SELECT * FROM Pic";
            OleDbCommand com = new OleDbCommand(sql, con);
            OleDbDataAdapter oda = new OleDbDataAdapter(com);
            DataTable dt = new DataTable();
            oda.Fill(dt);
            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "ID";
            comboBox1.ValueMember = "ID";
        }
        //public partial class Binary : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged
        //{

        //    [System.NonSerializedAttribute()]
        //    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

        //    [System.Runtime.Serialization.OptionalFieldAttribute()]
        //    private byte[] BytesField;

        //    [global::System.ComponentModel.BrowsableAttribute(false)]
        //    public System.Runtime.Serialization.ExtensionDataObject ExtensionData
        //    {
        //        get
        //        {
        //            return this.extensionDataField;
        //        }
        //        set
        //        {
        //            this.extensionDataField = value;
        //        }
        //    }

        //    [System.Runtime.Serialization.DataMemberAttribute()]
        //    public byte[] Bytes
        //    {
        //        get
        //        {
        //            return this.BytesField;
        //        }
        //        set
        //        {
        //            if ((object.ReferenceEquals(this.BytesField, value) != true))
        //            {
        //                this.BytesField = value;
        //                this.RaisePropertyChanged("Bytes");
        //            }
        //        }
        //    }

            public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

            protected void RaisePropertyChanged(string propertyName)
            {
                System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
                if ((propertyChanged != null))
                {
                    propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
                }
            }
        }
    
        //private void btn_Add_Click(object sender, EventArgs e)
        //{
        //    byte[] file_byte = ImageToByteArray(Image.FromFile(comboBox1.Text));

        //    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\DB1.accdb");
        //    con.Open();
        //    string sql = "INSERT INTO PIC(Pict) VALUES (" + file_byte + ")";
        //    OleDbCommand com = new OleDbCommand(sql, con);
        //    com.ExecuteNonQuery();
        //    MessageBox.Show("Oke!!!");
        //}

    }

